package com.example.userservice.novel.controller;


import com.example.userservice.novel.entity.NovelStructure;
import com.example.userservice.novel.service.EnhancedNovelService;
import com.example.userservice.novel.service.NovelStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/novel/storage")
public class NovelStorageController {

    @Autowired
    private NovelStorageService novelStorageService;

    @Autowired
    private EnhancedNovelService enhancedNovelService;

    @GetMapping("/list")
    public ResponseEntity<List<NovelStructure>> listNovels() {
        try {
            List<NovelStructure> novels = novelStorageService.findAll();
            return ResponseEntity.ok(novels);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 获取小说详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<NovelStructure> getNovelById(@PathVariable Long id) {
        try {
            NovelStructure novel = novelStorageService.findById(id);
            return ResponseEntity.ok(novel);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 搜索小说
     */
    @GetMapping("/search")
    public ResponseEntity<List<NovelStructure>> searchNovels(String novelName) {
        try {
            List<NovelStructure> novels = novelStorageService.searchByNovelName(novelName);
            return ResponseEntity.ok(novels);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 获取最近更新小说
     */
    @GetMapping("/recent")
    public ResponseEntity<List<NovelStructure>> getRecentNovels() {
        try {
            List<NovelStructure> novels = novelStorageService.findRecentNovels();
            return ResponseEntity.ok(novels);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 删除小说
     */
    @GetMapping("/delete/{id}")
    public ResponseEntity<Void> deleteNovel(@PathVariable Long id) {
        try {
            novelStorageService.deleteNovelStructure(id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }
}
